package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Resource
    private SearchFeignClient searchFeignClient;
    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;
    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @XxlJob("updateRanking")
    public void updateRanking(){

        XxlJobLog xxlJobLog = new XxlJobLog();

        long startTime = System.currentTimeMillis();

        try {

            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            searchFeignClient.updateLatelyAlbumRanking();
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            System.out.println(e.getMessage());
        }

        long endTime = System.currentTimeMillis();
        xxlJobLog.setTimes((int)(endTime-startTime));
        xxlJobLogMapper.insert(xxlJobLog);

        log.info("updateRanking");
    }

    @XxlJob("updateVipExpireStatusJob")
    public void updateVipExpire(){

        XxlJobLog xxlJobLog = new XxlJobLog();

        long startTime = System.currentTimeMillis();

        try {

            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            userInfoFeignClient.updateVipExpireStatus();
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            System.out.println(e.getMessage());
        }

        long endTime = System.currentTimeMillis();
        xxlJobLog.setTimes((int)(endTime-startTime));
        xxlJobLogMapper.insert(xxlJobLog);

        log.info("updateRanking");
    }

}